package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.PrefUtils;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 新手引导页
 * <p>
 * Created by Administrator on 2017/8/22.
 */

public class GuideActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.rl_jump)
    RelativeLayout rlJump;
    private ViewPager mViewPager;
    private LinearLayout llContainer;
    private ImageView ivReadPoint;//小蓝点
    private Button startBtn;//开始体验按钮

    private ACache mCache;

    //引导页面的图片id数组
    private int[] ids = {R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R.mipmap.p4};

    private List<ImageView> imageViews;//image集合

    //小红点移动的距离
    private int mPointDis;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_jump:

                //跳转到主页面
                startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();//结束当前页面
                break;
            case R.id.btn_start:
                // TODO Auto-generated method stub
                //更新sp,已经不是第一次进入了
                PrefUtils.setBoolean(GuideActivity.this, "is_user_guide_showed", true);

                //跳转到主页面
                startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();//结束当前页面
                break;
            default:
                break;
        }
    }

    private class MyPagerAdapter extends PagerAdapter {

        //总条数
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imageViews.size();
        }

        /**
         * 判断view 是否上面的instantiateItem方法中的返回值
         * <p>
         * view 显示中的某一个view
         * <p>
         * object instantiateItem 方法中的返回值
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            // TODO Auto-generated method stub
            return view == object;
        }

        /**
         * 和getView()功能相似
         * <p>
         * container 容器
         * <p>
         * position 位置
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            //获得图片
            ImageView view = imageViews.get(position);
            //添加到容器
            container.addView(view);
            //返回实例化对象
            return view;
        }

        //销毁某一条
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            container.removeView((View) object);
            //super.destroyItem(container, position, object);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        mViewPager = (ViewPager) findViewById(R.id.vp);
        llContainer = (LinearLayout) findViewById(R.id.ll_container);
        startBtn = (Button) findViewById(R.id.btn_start);

        initData();

        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * 当选择了某个页面的时候回调
             *
             * 就是选择了那个页面：position
             */
            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                //某个页面被选中
                if (position == imageViews.size() - 1) {//最后一个页面显示开始体验按钮
                    startBtn.setVisibility(View.VISIBLE);//显示
                } else {
                    startBtn.setVisibility(View.INVISIBLE);//隐藏
                }
                int myIndex = position;
                //当前的指示点设置为高亮
                llContainer.getChildAt(myIndex).setEnabled(true);
                //上的的高亮的，设置为默认
                llContainer.getChildAt(mPointDis).setEnabled(false);
                //记录上一次
                mPointDis = myIndex;
            }

            /**
             * 当前页面滚动了时候回调
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // TODO Auto-generated method stub
                //System.out.println("当前位置:"+position+"  移动偏移百分比:"+positionOffset);
            }

            /**
             * 当前页面状态发送变化的时候回调
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        startBtn.setOnClickListener(this);
        rlJump.setOnClickListener(this);

        mCache.put("mark","yes");
    }

    public void initData() {
        imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < ids.length; i++) {
            ImageView view = new ImageView(this);
            view.setBackgroundResource(ids[i]);
            imageViews.add(view);


            ImageView point = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            params.leftMargin = 40;
            point.setLayoutParams(params);
            point.setBackgroundResource(R.drawable.point_selector);//设置背景
            llContainer.addView(point);
            if (i == 0) {
                point.setEnabled(true);
            } else {
                point.setEnabled(false);
            }
        }
    }
}
